package com.example.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.auth.entity.SysUser;
import com.example.auth.entity.dto.LoginDTO;
import com.example.auth.exception.LyException;
import com.example.auth.service.LoginService;
import com.example.auth.service.SysUserService;
import com.example.auth.utils.JwtHelper;
import com.example.auth.utils.RedisUtil;
import com.example.auth.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: LiYang
 * @create: 2023-07-10 09:26
 * @Description:
 * @Version 1.0
 **/
@RestController
@RequestMapping("system/admin")
@Api(tags = "登录管理")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RedisUtil redisUtil;


    @PostMapping("login")
    @ApiOperation("登录")
    public Result login(@RequestBody LoginDTO loginDTO){
        Map<String, Object> map = loginService.login(loginDTO);
        return Result.success(map);
    }


    @GetMapping("info")
    @ApiOperation("获取用户信息")
    public Result getInfo(HttpServletRequest request){
        Map<String, Object> map = loginService.getInfo(request);

        return  Result.success(map);
    }

    //logout
    @PostMapping("logout")
    @ApiOperation("注销")
    public Result logout(HttpServletRequest request){
        String token = request.getHeader("Access-Token");
        Object userInfo = redisUtil.get(token);
        if(!ObjectUtils.isEmpty(userInfo)){
            redisUtil.del(token);
        }
        return Result.success();
    }

}
